import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/YouLogin.vue')
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('@/views/home/HomeView.vue'),
      children: [
        {
          path: 'coll',
          name: 'coll',
          component: () => import('@/views/home/CollaboraTRion.vue')
        },
        {
          path: 'myw',
          name: 'myw',
          component: () => import('@/views/home/MyWork.vue')
        },
        {
          path: 'hui',
          name: 'hui', //一级路由
          component: () => import('@/views/home/RecycleBin.vue')
        }
      ]
    },
    {
      path: '/index',
      name: 'index',
      component: () => import('@/views/center.vue')
    },

    {
      path: '/myb',
      name: 'myb', //一级路由
      component: () => import('@/views/MyWorkbench .vue')
    },
    {
      path: '/aaa',
      name: 'aaa', //一级路由
      component: () => import('../views/aaa.vue')
    },
    {
      path: '/bbb',
      name: 'bbb', //一级路由
      component: () => import('@/views/bbb.vue')
    },
    {
      path: '/cb',
      name: 'cb', //一级路由
      component: () => import('@/views/cb.vue')
    },
    {
      path: '/y',
      name: 'y', //一级路由
      component: () => import('@/views/YanSe.vue')
    },
    {
      path: '/yy',
      name: 'yy', //一级路由
      component: () => import('@/views/ZoomSize.vue')
    },
     {
      path: '/pizhu',
      name: 'pizhu', //一级路由
      component: () => import('@/views/PiZhu.vue')
    },
      {
      path: '/comment',
      name: 'comment', //一级路由
      component: () => import('@/views/comment.vue')
    }
  
  ]
})
// 白名单，不需要token即可访问的路由
const whiteList = ['/login', '/register', '/home'] // 示例路径
// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 检查当前路由是否在白名单中
  if (whiteList.indexOf(to.path) !== -1) {
    // 在白名单中，直接放行
    next()
  } else {
    // 检查是否存在token，这里假设token存储在localStorage中
    const token = sessionStorage.getItem('token')
    if (token) {
      // 存在token，放行
      next()
    } else {
      // 没有token，重定向到登录页面
      next({ path: '/login' })
      // 或者你可以使用next(false)来取消导航
    }
  }
})

export default router
